/**  
 * All rights Reserved, Designed By www.richfit.com
 * @Title:  I18nUtil.java   
 * @Package cn.eonshine.base.config   
 * @Description:    TODO
 * @author: lanjf2000
 * @date:   2018-10-22 13:42:02
 * @version V1.0 
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
package cn.eonshine.base.core.beetl;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.core.exception.BeetlException;

import cn.eonshine.base.core.util.LocaleStringUtil;

/**   
 * @ClassName:  I18nUtil   
 * @Description:TODO
 * @author: lanjf2000
 * @date:   2018-10-22 13:42:02
 *     
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
public class I18nUtil implements Function {

	public static String LOCALE_PARAM="lang";
	/**   
	 * <p>Title: call</p>   
	 * <p>Description: </p>   
	 * @param obj
	 * @param context
	 * @return   
	 * @see org.beetl.core.Function#call(java.lang.Object[], org.beetl.core.Context)   
	 */
	@Override
	public Object call(Object[] obj, Context context) {
		if(obj.length < 1) {
            try {
                throw new BeetlException("调用Beetl方法获取国际化信息时 参数列表传递错误");
            } catch (BeetlException e) {
                e.printStackTrace();
                return "调用Beetl方法获取国际化信息时 参数列表传递错误";
            }
        }
//        HttpServletRequest request = (HttpServletRequest) context.getGlobal("request");
//        String locale = request.getParameter(LOCALE_PARAM);
//        if(StringUtils.isNotBlank(locale)) {
//        	UserUtils.setUserLocal(locale);
//        }
//        RequestContext requestContext = new RequestContext(request);
        String message = "";
        try {
        	message = LocaleStringUtil.getMessage((String)obj[0]);
//            message = requestContext.getMessage((String)obj[0]);
            if("".equals(message) && obj.length > 1){
                message = (String)obj[(int)(1+Math.random()*(obj.length-1))];
            }
        } catch (Exception e) {
            e.printStackTrace();
            if(obj.length > 1)
                message = (String)obj[(int)(1+Math.random()*(obj.length-1))];
        }
        return message;
	}

}
